package com.lambda.swipeback;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;


public class MMFragmentActivity extends AppCompatActivity implements SwipeBackLayout.SwipeGestureDelegate, SwipeBackHelper.SwipeCallback {
    private static final String TAG = "MicroMsg.MMFragmentActivity";
    public static final String EXTRA_USE_SYSTEM_DEFAULT_ENTER_EXIT_ANIM = "extra_use_system_default_enter_exit_anim";

    private View mContentViewForSwipeBack = null;
    private SwipeBackLayout mSwipeBackLayout;
    private boolean mSwiping;

    @Override
    protected void onResume() {
        super.onResume();
        if (isSupportNavigationSwipeBack()) {
            SwipeBackHelper.pop(this);
            onSwipe(1f);
            SwipeBackLayout layout = getSwipeBackLayout();
            if (layout != null) {
                layout.setEnableGesture(true);
                layout.mSwiping = false;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isSupportNavigationSwipeBack()) {
            if (getSwipeBackLayout() != null) {
                getSwipeBackLayout().setEnableGesture(false);
            }
            if (!isFinishing()) {
                SwipeBackHelper.push(this);
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        if (isSupportNavigationSwipeBack()) {
            SwipeBackLayout swipeBackLayout = getSwipeBackLayout();
            if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && swipeBackLayout != null && swipeBackLayout.swipeBack()) {
                ad.w("ashutest", "ashutest::IS SwipeBack ING, ignore KeyBack Event");
                return true;
            }
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    public SwipeBackLayout getSwipeBackLayout() {
        return this.mSwipeBackLayout;
    }

    public boolean initNavigationSwipeBack() {
        if (CheckSDK.kW(19)) {
            if (!ActivityUtil.aX(getClass()) || !convertActivityFromTranslucent()) {
                boolean z;
                if ((ActivityUtil.aV(getClass()) & 16) != 0) {
                    z = true;
                } else {
                    z = false;
                }
                if (z && ToolsAQ.isMainThread()) {
                    ActivityUtil.bh(this);
                }
            } else if (ToolsAQ.isMainThread()) {
                ActivityUtil.bh(this);
            }
        }
        if (isSupportNavigationSwipeBack()) {
            initSwipeBack();
            return true;
        }
        return false;
    }

    protected void initSwipeBack() {
        ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
        this.mSwipeBackLayout = (SwipeBackLayout) LayoutInflater.from(this).inflate(R.layout.layout_swipe_back, viewGroup, false);
        this.mSwipeBackLayout.init();
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        getWindow().getDecorView().setBackgroundDrawable(null);
        View childAt = viewGroup.getChildAt(0);
        childAt.setBackgroundResource(R.color.a_q);
        viewGroup.removeView(childAt);
        this.mSwipeBackLayout.addView(childAt);
        this.mSwipeBackLayout.setContentView(childAt);
        viewGroup.addView(this.mSwipeBackLayout);
        this.mSwipeBackLayout.setSwipeGestureDelegate(this);
    }

    public final boolean isSupportNavigationSwipeBack() {
        if (!CheckSDK.kW(19) || !CompatHB.Xx()) {
            return false;
        } else if (supportNavigationSwipeBack() && ActivityUtil.aX(getClass())) {
            return true;
        } else {
            return false;
        }
    }

    public boolean supportNavigationSwipeBack() {
        return true;
    }

    protected boolean convertActivityFromTranslucent() {
        return true;
    }

    @Override
    public void onCancel() {
        this.mSwiping = false;
    }

    @Override
    public void onDrag() {
        this.mSwiping = true;
    }

    @Override
    public void onSwipeBack() {
        if (!isFinishing()) {
            finish();
        }
        getWindow().getDecorView().setVisibility(View.GONE);
        overridePendingTransition(0, 0);
        this.mSwiping = false;
    }

    @Override
    public boolean forceRemoveNoMatchOnPath() {
        return false;
    }

    @Override
    public void onSettle(boolean z, int i) {
        long j = 160;
        if (this.mContentViewForSwipeBack == null) {
            this.mContentViewForSwipeBack = UIUtils.a(getWindow(), getSupportActionBar().getCustomView());
        }
        if (z) {
            if (i <= 0) {
                j = 320;
            }
            ToolsJ.a(this.mContentViewForSwipeBack, j, 0.0f, null);
            return;
        }
        if (i <= 0) {
            j = 320;
        }
        ToolsJ.a(this.mContentViewForSwipeBack, j, ((float) (this.mContentViewForSwipeBack.getWidth() * -1)) / 3.5f, null);
    }

    @Override
    public void onSwipe(float percent) {
        ad.v("ashutest", "ashutest::on swipe %f, duration %d", percent, 320L);
        if (mContentViewForSwipeBack == null) {
            mContentViewForSwipeBack = UIUtils.a(getWindow(), getSupportActionBar() != null ? getSupportActionBar().getCustomView() : null);
        }
        if (Float.compare(1.0f, percent) <= 0) {
            ToolsJ.w(mContentViewForSwipeBack, 0.0f);
            return;
        }
        ToolsJ.w(mContentViewForSwipeBack, ((((float) mContentViewForSwipeBack.getWidth()) / 3.5f) * (1.0f - percent)) * -1.0f);
    }

    public boolean enableActivityAnimation() {
        boolean b;
        Intent intent = getIntent();
        if (intent != null) {
            b = intent.getBooleanExtra(EXTRA_USE_SYSTEM_DEFAULT_ENTER_EXIT_ANIM, false);
        } else {
            b = false;
        }
        return !b;
    }

    protected void initActivityOpenAnimation(Intent intent) {
        if (enableActivityAnimation()) {
            ComponentName component = intent == null ? null : intent.getComponent();
            if (component != null) {
                String e = ActivityUtil.e(component);
                if (ActivityUtil.aET(e)) {
                    super.overridePendingTransition(a.EiM, a.EiN);
                } else if (ActivityUtil.aEU(e)) {
                    ActivityUtil.jl(this);
                } else {
                    ActivityUtil.ji(this);
                }
            }
        }
    }

    protected void initActivityCloseAnimation() {
        if (enableActivityAnimation()) {
            if (ActivityUtil.aY(getClass())) {
                super.overridePendingTransition(a.EiO, a.EiP);
                return;
            }
            if ((ActivityUtil.aV(getClass()) & 4) != 0) {
                ActivityUtil.jl(this);
            } else {
                ActivityUtil.jk(this);
            }
        }
    }

    public void startActivity(Intent intent) {
        super.startActivity(intent);
        initActivityOpenAnimation(intent);
    }

    public void finish() {
        super.finish();
        initActivityCloseAnimation();
    }

    public static final class a {
        public static int ldi;
        public static int ldj;
        public static int ldk;
        public static int ldl;
        public static int EiM = ldi;
        public static int EiN = ldj;
        public static int EiO = ldk;
        public static int EiP = ldl;

        public static void eBj() {
            int i = R.anim.s;
            int i2;
            boolean Xx = CompatHB.Xx() & CheckSDK.kW(19);
            ad.i(MMFragmentActivity.TAG, "lm: setAnimationStyle supportSwipe = ".concat(String.valueOf(Xx)));
            ldi = Xx ? R.anim.dw : R.anim.ck;
            if (Xx) {
                i2 = R.anim.dt;
            } else {
                i2 = R.anim.s;
            }
            ldj = i2;
            if (Xx) {
                i = R.anim.ds;
            }
            ldk = i;
            ldl = Xx ? R.anim.dx : R.anim.cp;
            EiM = ldi;
            EiN = ldj;
            EiO = ldk;
            EiP = ldl;
        }

        static {
            int i;
            int i2 = R.anim.s;
            boolean Xx = CompatHB.Xx() & CheckSDK.kW(19);
            ldi = Xx ? R.anim.dw : R.anim.ck;
            if (Xx) {
                i = R.anim.dt;
            } else {
                i = R.anim.s;
            }
            ldj = i;
            if (Xx) {
                i2 = R.anim.ds;
            }
            ldk = i2;
            if (Xx) {
                i = R.anim.dx;
            } else {
                i = R.anim.cp;
            }
            ldl = i;
        }
    }
}
